package com.utils.info.util.hhy;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Hashtable;

/**
 * 二维码生成工具类
 *
 * @author hhy
 * @date 2021-12-17 15:01:56
 * @desc 描述： 通过二维码扫描跳转指指定网页
 */
@SuppressWarnings("ALL")
public class QRCodeUtil {

    private static final String CHARSET = "utf-8";
    // 二维码尺寸
    private static final int QRCODE_SIZE = 300;
    // LOGO宽度
    private static final int WIDTH = 60;
    // LOGO高度
    private static final int HEIGHT = 60;

    /**
     * 获取生成二维码的图片流
     *
     * @param url          跳转地址
     * @param imgPath      logo图片地址
     * @param needCompress 是否压缩logo图片
     * @return 返回二维码生成后的图片流
     */
    @SneakyThrows
    public static ByteArrayOutputStream encodeIO(String url, String imgPath, Boolean needCompress) {
        Hashtable<EncodeHintType,Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        if (StringUtils.isNotEmpty(imgPath)) {
            // 插入图片
            File file = new File(imgPath);
            if (!file.exists()) {
                System.err.println("" + imgPath + "   该文件不存在！");
                return null;
            }
            Image src = ImageIO.read(new File(imgPath));
            int widthImage = src.getWidth(null);
            int heightImage = src.getHeight(null);
            if (needCompress) { // 压缩LOGO
                if (widthImage > WIDTH) {
                    widthImage = WIDTH;
                }
                if (heightImage > HEIGHT) {
                    heightImage = HEIGHT;
                }
                Image source = src.getScaledInstance(widthImage, heightImage, Image.SCALE_SMOOTH);
                BufferedImage tag = new BufferedImage(widthImage, heightImage, BufferedImage.TYPE_INT_RGB);
                Graphics g = tag.getGraphics();
                g.drawImage(source, 0, 0, null); // 绘制缩小后的图
                g.dispose();
                src = source;
            }
            // 插入LOGO
            Graphics2D graph = image.createGraphics();
            int x = (QRCODE_SIZE - widthImage) / 2;
            int y = (QRCODE_SIZE - heightImage) / 2;
            graph.drawImage(src, x, y, widthImage, heightImage, null);
            Shape shape = new RoundRectangle2D.Float(x, y, widthImage, widthImage, 6, 6);
            graph.setStroke(new BasicStroke(3f));
            graph.draw(shape);
            graph.dispose();
        }
        //创建储存图片二进制流的输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //将二进制数据写入ByteArrayOutputStream
        ImageIO.write(image, "jpg", baos);
        return baos;
    }
}
